package org.dey.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.mapper.DietaryMapper;
import org.dey.pojo.Dietary;
import org.dey.pojo.Exercise;
import org.dey.pojo.PageVo;
import org.dey.service.DietaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DietaryServiceimpl extends ServiceImpl<DietaryMapper, Dietary> implements DietaryService {
    @Autowired(required = false)
    DietaryMapper dietaryMapper;

    @Override
    public PageVo<Dietary> queryDietar(Dietary dietary,int pageNum, int pageSize) {
        //组装条件
        QueryWrapper<Dietary> queryWrapper = new QueryWrapper<Dietary>();

        //根据名字模糊查询
        if(StringUtils.isNotBlank(dietary.getFoodname())) {
            queryWrapper.like("foodname", dietary.getFoodname());
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Dietary> dietaries = this.list(queryWrapper);

        PageInfo<Dietary> pageInfo = new PageInfo<Dietary>(dietaries);
        PageVo<Dietary> pageVo =new PageVo<Dietary>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());
        return pageVo;
    }

    @Override
    public int insertDietar(Dietary dietary) {
        int num = 0;

        QueryWrapper<Dietary> queryWrapper = new QueryWrapper<Dietary>();
        queryWrapper.eq("foodname", dietary.getFoodname());

        Dietary one = this.getOne(queryWrapper);

        if(one==null){
            boolean save = this.save(dietary);
            if(save){
                num=1;
            }
        }
        return num;
    }

    @Override
    public int deleteDietar(int id) {
        int num = 0;
        boolean b = this.removeById(id);
        if(b){
            num = 1;
        }
        return num;
    }

    @Override
    public Dietary queryDietarByid(int id) {
        return this.getById(id);
    }

    @Override
    public int updateDietar(Dietary dietary) {
        int num = 0;
        boolean b = this.updateById(dietary);
        if(b){
            num = 1;
        }
        return num;
    }
}
